// GlobalStatusView.m
#import "GlobalStatusView.h"

@interface GlobalStatusView ()
@property (nonatomic, assign) BOOL isInitialized;
@end

@implementation GlobalStatusView

+ (instancetype)sharedInstance {
    static GlobalStatusView *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        // 不要在这里初始化frame，在show方法中初始化
        instance = [[GlobalStatusView alloc] init];
        instance.isInitialized = NO;
    });
    return instance;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        // 延迟初始化UI
        self.windowLevel = UIWindowLevelStatusBar + 100; // 设置更高的层级
        self.backgroundColor = [UIColor clearColor];
        self.hidden = YES;
        self.userInteractionEnabled = NO;
        self.rootViewController = [[UIViewController alloc] init]; // 关键：必须设置rootViewController
    }
    return self;
}

- (void)setupUI {
    if (self.isInitialized) return;
    
    CGFloat topSafeArea = [self getTopSafeArea];
    CGFloat statusHeight = [UIScreen mainScreen].bounds.size.height*0.13;
    CGRect frame = CGRectMake(0, topSafeArea, [UIScreen mainScreen].bounds.size.width, statusHeight);
    self.frame = frame;
    UIImageView *bgIView=[[UIImageView alloc]initWithFrame:self.bounds];
      bgIView.image=[UIImage imageNamed:@"statusviewbgimage"];
    bgIView.alpha = 0;
      [self addSubview:bgIView];
    UIView *contentView = [[UIView alloc] initWithFrame:self.bounds];
 
    [self addSubview:contentView];

    // 头像
    self.avatarImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height*0.13*0.78, [UIScreen mainScreen].bounds.size.height*0.13)];
    self.avatarImageView.image = [UIImage imageNamed:@"Main_Story_Battle"];
    self.avatarImageView.hidden = true;

    self.avatarImageView.contentMode = UIViewContentModeScaleAspectFill;
    [contentView addSubview:self.avatarImageView];
    
    // 钻石
//    UIImageView *diamondIcon = [[UIImageView alloc] initWithFrame:CGRectMake(60, 15, 20, 20)];
//    diamondIcon.image = [UIImage imageNamed:@"diamond_icon"];
//    diamondIcon.contentMode = UIViewContentModeScaleAspectFit;
//    [contentView addSubview:diamondIcon];
//    
//    self.diamondLabel = [[UILabel alloc] initWithFrame:CGRectMake(85, 15, 60, 20)];
//    self.diamondLabel.textColor = [UIColor whiteColor];
//    self.diamondLabel.font = [UIFont systemFontOfSize:14];
//    self.diamondLabel.text = @"0";
//    [contentView addSubview:self.diamondLabel];
    
    self.mddiamondBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    self.mddiamondBtn.frame=CGRectMake(self.frame.size.width-30-self.frame.size.height*0.37*4.86, 0,  self.frame.size.height*0.37*2.86, self.frame.size.height*0.37);
    [self.mddiamondBtn setBackgroundImage:[UIImage imageNamed:@"钻石status"] forState:UIControlStateNormal];
    [self.mddiamondBtn setTitle:[NSString stringWithFormat:@"99%ld",[[NSUserDefaults standardUserDefaults]integerForKey:@"mddiamond"]] forState:UIControlStateNormal];
    self.mddiamondBtn.font = [UIFont systemFontOfSize:13];
    [self.mddiamondBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [contentView addSubview:self.mddiamondBtn];
    self.mddiamondBtn.titleEdgeInsets=UIEdgeInsetsMake(0, 15, 0, 0);
    // 金币
    self.goldiconBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    self.goldiconBtn.frame=CGRectMake(self.frame.size.width-30-self.frame.size.height*0.37*7.86, 0,  self.frame.size.height*0.37*2.86, self.frame.size.height*0.37);
    [self.goldiconBtn setBackgroundImage:[UIImage imageNamed:@"金币status"] forState:UIControlStateNormal];
    [self.goldiconBtn setTitle:[NSString stringWithFormat:@"99%ld",[[NSUserDefaults standardUserDefaults]integerForKey:@"mddiamond"]] forState:UIControlStateNormal];
    self.goldiconBtn.font = [UIFont systemFontOfSize:13];
    [self.goldiconBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [contentView addSubview:self.goldiconBtn];
    self.goldiconBtn.titleEdgeInsets=UIEdgeInsetsMake(0, 15, 0, 0);
    
    
//    UIImageView *goldIcon = [[UIImageView alloc] initWithFrame:CGRectMake(150, 15, 20, 20)];
//    goldIcon.image = [UIImage imageNamed:@"goldicon"];
//    goldIcon.contentMode = UIViewContentModeScaleAspectFit;
//    [contentView addSubview:goldIcon];
    
//    self.goldLabel = [[UILabel alloc] initWithFrame:CGRectMake(175, 15, 60, 20)];
//    self.goldLabel.textColor = [UIColor whiteColor];
//    self.goldLabel.font = [UIFont systemFontOfSize:14];
//    self.goldLabel.text = @"0";
//    [contentView addSubview:self.goldLabel];
    
    // 物品
    UIImageView *itemIcon = [[UIImageView alloc] initWithFrame:CGRectMake(240, 15, 20, 20)];
    itemIcon.image = [UIImage imageNamed:@"diamondglobal"];
    itemIcon.contentMode = UIViewContentModeScaleAspectFit;
//    [contentView addSubview:itemIcon];
    
    self.itemLabel = [[UILabel alloc] initWithFrame:CGRectMake(265, 15, 60, 20)];
    self.itemLabel.textColor = [UIColor whiteColor];
    self.itemLabel.font = [UIFont systemFontOfSize:14];
    NSUserDefaults *deff=[NSUserDefaults standardUserDefaults];
    if (![deff boolForKey:@"ismdfirst"]) {
        [deff setBool:YES forKey:@"ismdfirst"];
        [deff setInteger:3000 forKey:@"mddiamond"];
    }
    
//    [contentView addSubview:self.itemLabel];
    
    self.isInitialized = YES;
}

- (CGFloat)getTopSafeArea {
    if (@available(iOS 11.0, *)) {
        UIWindow *window = UIApplication.sharedApplication.windows.firstObject;
        return window.safeAreaInsets.top;
    }
    return 20.0; // 状态栏高度
}

#pragma mark - 公共方法

- (void)show {
    // 确保在主线程执行
    dispatch_async(dispatch_get_main_queue(), ^{
        [self setupUI];
        self.hidden = NO;
        [self makeKeyAndVisible];
        
        // 恢复主窗口为keyWindow
        [UIApplication.sharedApplication.delegate.window makeKeyWindow];
    });
}

- (void)hide {
    dispatch_async(dispatch_get_main_queue(), ^{
        self.hidden = YES;
    });
}

- (void)updateDiamondCount:(NSInteger)count {
//    self.diamondLabel.text = [NSString stringWithFormat:@"%ld", (long)count];
//    [self animateUpdateForLabel:self.diamondLabel];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.mddiamondBtn setTitle:[NSString stringWithFormat:@"%ld",[[NSUserDefaults standardUserDefaults]integerForKey:@"mddiamond"]] forState:UIControlStateNormal];
        [self.mddiamondBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    });
}

- (void)updateGoldCount:(NSInteger)count {
    self.goldLabel.text = [NSString stringWithFormat:@"%ld", (long)count];
    [self animateUpdateForLabel:self.goldLabel];
}

- (void)updateItemCount:(NSInteger)count {
    self.itemLabel.text = [NSString stringWithFormat:@"%ld", (long)count];
    [self animateUpdateForLabel:self.itemLabel];
}

- (void)animateUpdateForLabel:(UILabel *)label {
    [UIView animateWithDuration:0.1 animations:^{
        label.transform = CGAffineTransformMakeScale(1.2, 1.2);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.1 animations:^{
            label.transform = CGAffineTransformIdentity;
        }];
    }];
}

@end
